//
//  SNOrderFormDetailGoodsCell.m
//  app
//
//  Created by 王少宁 on 15/5/18.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "SNOrderFormDetailGoodsCell.h"

@interface SNOrderFormDetailGoodsCell ()
@property (nonatomic, retain) UIImageView * goodsImageView;
@property (nonatomic, retain) UILabel * titleLabel;
@property (nonatomic, retain) UILabel * moneyLabel;
@property (nonatomic, retain) UILabel * countLabel;
@property (nonatomic, retain) UIButton * goodsBtn;
@property (nonatomic, retain) UIButton * commentBtn;
@property (nonatomic, retain) UIButton * returnGoodsBtn;
@end

@implementation SNOrderFormDetailGoodsCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _goodsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_goodsBtn addTarget:self action:@selector(goodsBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_goodsBtn];
        [self crateCell];
        
        _returnGoodsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:_returnGoodsBtn];
        _commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:_commentBtn];
    }
    return self;
}

- (void)crateCell
{
    _goodsImageView = CREATE_WITH_FRAME_OF(UIImageView, CGPointMake(10, 16),
                                           [self.contentView.ubo ow:64 oh:64]);
    _goodsImageView.layer.borderWidth = UP(1);
    _goodsImageView.layer.borderColor = [XCommon hexStringToColor:kColor_gray_line].CGColor;
    [self.contentView addSubview:_goodsImageView];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = JKAppGlobleFontSmallTagText;
    _titleLabel.textColor = [XCommon hexStringToColor:kColor_l_light_gray_text];
    _titleLabel.numberOfLines = 0;
    [self.contentView addSubview:_titleLabel];
    
    _moneyLabel = [[UILabel alloc] init];
    _moneyLabel.font = [UIFont boldSystemFontOfSize:13];
    _moneyLabel.textColor = [XCommon hexStringToColor:kColor_main_theme_orange];
    [self.contentView addSubview:_moneyLabel];
    
    _countLabel = [[UILabel alloc] init];
    _countLabel.font = JKAppGlobleFontSmallTagText;
    _countLabel.textColor = [XCommon hexStringToColor:kColor_s_light_gray_text];
    _countLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:_countLabel];
}

- (void)cellWithModel:(SNOrderFormDetailGoodsModel *)model row:(NSInteger)row
{
    _goodsBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH, model.cellHeight);
    self.row = row;
    self.detailGoodsModel = model;
    [_goodsImageView sd_setImageWithURL:[NSURL URLWithString:model.title_pic]];
    CGFloat height = [XCommon heightForString:model.product_title fontSize:13 andWidth:SCREEN_WIDTH-UF(24)-10-_goodsImageView.ufo.xx];
    _titleLabel.frame = CGRectOf([_goodsImageView.ufo fx:1.0 ox:10 fy:0],
                                 [self.contentView.ubo ow:SCREEN_WIDTH - _goodsImageView.ubo.width - 30 oh:height]);
    _titleLabel.text = model.product_title;
    NSString * moneyText = [NSString stringWithFormat:@"￥%@",model.price];;
    _moneyLabel.text = moneyText;
    [_moneyLabel sizeToFit];
    _moneyLabel.ufo.origin = [_goodsImageView.ufo fx:1.0 ox:10 fy:1.0 oy:-20];
    _countLabel.text = [NSString stringWithFormat:@"×%@",model.product_num];
    
    if (model.returnGoodsBtnType == ReturnGoodsNone && !model.includeCommentBtn) {
        _returnGoodsBtn.hidden = _commentBtn.hidden = YES;
        _countLabel.frame = CGRectOf([self.contentView.ufo ox:SCREEN_WIDTH-50-UF(24) oy:_moneyLabel.ufo.y],
                             [_moneyLabel.ubo ow:50 fh:1.0]);
    } else {
        [self createReturnGoodsBtnWithReturnGoodsBtnType:model.returnGoodsBtnType];
        [self createCommentBtnWithIncludeCommentBtn:model.includeCommentBtn];
    }
    
    if (model.giftModel.gift) {
        //    SNGiftAndPromotionView * giftAndPromotionView = [[SNGiftAndPromotionView alloc] initWithOriginY:90 giftModel:model.giftModel promotionModel:nil];
        SNGiftAndPromotionView * giftAndPromotionView = [[SNGiftAndPromotionView alloc] initWithFrame:CGRectZero];
        giftAndPromotionView.targetCallerShowGiftList = [UBTargetCaller of:self action:@selector(giftAndPromotionViewClick)];
        [giftAndPromotionView showViewWithOriginY:90 giftModel:model.giftModel];
        [self addSubview:giftAndPromotionView];
    }
}

- (void)createReturnGoodsBtnWithReturnGoodsBtnType:(ReturnGoodsBtnType)returnGoodsBtnType
{
    _countLabel.frame = CGRectOf([self.contentView.ufo ox:SCREEN_CENTER.x-50 oy:_moneyLabel.ufo.y],
                                 [_moneyLabel.ubo ow:50 fh:1.0]);
    
    if (returnGoodsBtnType == ReturnGoodsNone) {
        #warning - 其实在没有 申请售后 按钮的情况下.下面这句代码可以不写,直接return就可以的.但为了保险起见还是为 申请售后 按钮写了一个默认的位置,此默认的宽度为0.
        _returnGoodsBtn.frame = CGRectMake(SCREEN_WIDTH - UF(24) - 60, _goodsImageView.ufo.yy-25, 0, 25);
        return;
    }
    
    _returnGoodsBtn.hidden = NO;
    _returnGoodsBtn.frame = CGRectMake(SCREEN_WIDTH - UF(24) - 60, _goodsImageView.ufo.yy-25, 60, 25);
    [_returnGoodsBtn setTitle:kReturnGoodsBtnTitle forState:UIControlStateNormal];
    [_returnGoodsBtn setTitleColor:[XCommon hexStringToColor:kColor_s_light_gray_text] forState:UIControlStateNormal];
    _returnGoodsBtn.titleLabel.font = JKAppGlobleFontSmallTagText;
    _returnGoodsBtn.layer.borderWidth = UP(2);
    _returnGoodsBtn.layer.cornerRadius = 3;
    
//    if (returnGoodsBtnType == ReturnGoodsReturnApply) {//初次申请售后
        _returnGoodsBtn.layer.borderColor = [XCommon hexStringToColor:kColor_gray_line].CGColor;
//    } else if (returnGoodsBtnType == ReturnGoodsReturnAdd) {//追加申请售后原因
#warning - 同样是为了测试,在这里改变了 申请售后 按钮的颜色 : 初次申请售后为灰色,追加售后原因为红色.
//        _returnGoodsBtn.layer.borderColor = [XCommon hexStringToColor:kColor_gray_line].CGColor;
//    }
    
    [_returnGoodsBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)createCommentBtnWithIncludeCommentBtn:(BOOL)includeCommentBtn
{
    if (!includeCommentBtn) {
        return;
    }
    
    _commentBtn.hidden = NO;
    _commentBtn.frame = CGRectMake(_returnGoodsBtn.ufo.x - 10 - 60, _returnGoodsBtn.ufo.y, 60, 25);
    [_commentBtn setTitle:kCommentBtnTitle forState:UIControlStateNormal];
    [_commentBtn setTitleColor:[XCommon hexStringToColor:kColor_s_light_gray_text] forState:UIControlStateNormal];
    _commentBtn.titleLabel.font = JKAppGlobleFontSmallTagText;
    _commentBtn.layer.borderWidth = UP(2);
    _commentBtn.layer.cornerRadius = 3;
    _commentBtn.layer.borderColor = [XCommon hexStringToColor:kColor_gray_line].CGColor;
    [_commentBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
}

//点击了 商品 cell
- (void)goodsBtnClick
{
    if ([_deletage respondsToSelector:@selector(clickGoodsBtn:)]) {
        [_deletage performSelector:@selector(clickGoodsBtn:) withObject:self.detailGoodsModel];
    }
}

//点击了 评价 或者 申请退换货 按钮
- (void)clickBtn:(UIButton *)btn
{
    if ([_deletage respondsToSelector:@selector(clickBtnWithTitle:row:)]) {
        [_deletage clickBtnWithTitle:btn.currentTitle row:_row];
    }
}

/** 点击了赠品*/
- (void)giftAndPromotionViewClick
{
    if ([self.deletage respondsToSelector:@selector(clickGiftBtnWithDetailGoodsModel:)]) {
        [self.deletage performSelector:@selector(clickGiftBtnWithDetailGoodsModel:) withObject:self.detailGoodsModel];
    }
}

- (void)drawRect:(CGRect)rect
{
    if (self.row != 0) {
        [self drawTopLineLeft:10 right:0 color:[XCommon hexStringToColor:kColor_s_separator_cell_line]];
    }
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
